﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using SapB1WebPOS.BLL;
using SapB1WebPOS.Utils;
using SapB1WebPOS.Model;
using Newtonsoft.Json;

namespace SapB1WebPOS
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string ChangeUserPassword(string newPassword)
        {
            try
            {
                string msg = string.Empty;
                PermissionBLL pmt = new PermissionBLL();
                string sapUser = string.Empty;
                string companyDB = string.Empty;
                if (HttpContext.Current.Session["LoginUserName"] != null)
                {
                    sapUser = HttpContext.Current.Session["LoginUserName"].ToString();
                }
                if (HttpContext.Current.Session["CompanyDB"] != null)
                {
                    companyDB = HttpContext.Current.Session["CompanyDB"].ToString();
                }
                string count = pmt.OnlineUserChangePassword(companyDB, sapUser, newPassword);
                if (Utils.Utils.IsNumber(count))
                {
                    if (int.Parse(count) > 0)
                    {
                        if (HttpContext.Current.Session["UserPermission"] != null)
                        {
                            OnlineUser user = (OnlineUser)HttpContext.Current.Session["UserPermission"];
                            user.U_Password = newPassword;
                            HttpContext.Current.Session["UserPermission"] = user;
                            return "SUCCESS";
                        }
                    }
                }
                return "ERROR";
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return ex.Message;
            }
        }
    }
}